package com.example.filter.config;

import com.example.filter.filter.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//过滤器分为  注解过滤器  和  非注解（@Bean过滤器）
   //注解过滤器  MyFilter 添加  @WebFilter(urlPatterns = "/api/*",filterName = "myFilter")  @Order(1)
   //非注解过滤器  FilterConfig 添加 @Bean getFilterRegistrationBean     以及 FilterApplication 添加@ServletComponentScan
@Configuration
public class FilterConfig {

    //指向我们自定义过滤器
    @Bean
    public MyFilter myFilter(){
        return new MyFilter();
    }

    @Bean
    public FilterRegistrationBean getFilterRegistrationBean(MyFilter myFilter){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(myFilter);
        filterRegistrationBean.setOrder(1);
        filterRegistrationBean.addUrlPatterns("/api/*");
        filterRegistrationBean.setName("myFilter");
        System.out.println("请求的接口=");
        return filterRegistrationBean;
    }
}
